package cn.worldyao.demo.util;

/**
 * @author yaozi
 */
public class ReturnSet<T> {
    private String message;
    private int code;
    private T data;

    public T getData() {
        return data;
    }

    public String getMessage() {
        return message;
    }

    public int getCode() {
        return code;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public void setData(T data) {
        this.data = data;
    }

    private ReturnSet(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    private ReturnSet(ReturnCode cm) {
        if (cm == null) {
            return;
        }
        this.code = cm.getCode();
        this.message = cm.getMessage();
    }

    /**
     * 成功时候的调用
     *
     * @return
     */
    public static <T> ReturnSet<T> success(T data) {
        return new ReturnSet<T>(200, "success", data);
    }

    public static <T> ReturnSet<T> fail(T data) {
        return new ReturnSet<T>(400, "fail", data);
    }

    /**
     * 成功，不需要传入参数
     *
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> ReturnSet<T> success() {
        return (ReturnSet<T>) success("");
    }

    @SuppressWarnings("unchecked")
    public static <T> ReturnSet<T> fail() {
        return (ReturnSet<T>) fail("");
    }

    /**
     * 失败时候的调用
     *
     * @return
     */
    public static <T> ReturnSet<T> error(ReturnCode cm) {
        return new ReturnSet<T>(cm);
    }

    /**
     * 失败时候的调用,扩展消息参数
     *
     * @param cm
     * @param msg
     * @return
     */
    public static <T> ReturnSet<T> error(ReturnCode cm, String msg) {
        cm.setMessage(cm.getMessage() + "--" + msg);
        return new ReturnSet<T>(cm);
    }

}
